package cn.stylefeng.guns.modular.business.frequency.cmcity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.cmcity.entity.CmCity;
import cn.stylefeng.guns.modular.business.frequency.cmcity.enums.CmCityExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.cmcity.mapper.CmCityMapper;
import cn.stylefeng.guns.modular.business.frequency.cmcity.model.param.CmCityParam;
import cn.stylefeng.guns.modular.business.frequency.cmcity.service.CmCityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * cm_city 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class CmCityServiceImpl extends ServiceImpl<CmCityMapper, CmCity> implements CmCityService {


    @Override
    public PageResult<CmCity> page(CmCityParam cmCityParam) {

        // 构造条件
        LambdaQueryWrapper<CmCity> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<CmCity> list(CmCityParam cmCityParam) {

        // 构造条件
        LambdaQueryWrapper<CmCity> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(CmCityParam cmCityParam) {

        // 校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(cmCityParam, false);

        // 将dto转为实体
        CmCity cmCity = new CmCity();
        BeanUtil.copyProperties(cmCityParam, cmCity);

        this.save(cmCity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CmCityParam cmCityParam) {
        this.removeById(cmCityParam.getCode());
    }

    @Override
    public void edit(CmCityParam cmCityParam) {

        // 根据id查询实体
        CmCity cmCity = this.queryCmCity(cmCityParam);

        // 校验参数，检查是否存在重复的编码或者名称，排除当前编辑的这条记录
        checkParam(cmCityParam, true);

        // 请求参数转化为实体
        BeanUtil.copyProperties(cmCityParam, cmCity);

        this.updateById(cmCity);
    }

    @Override
    public CmCity detail(CmCityParam cmCityParam) {
        return this.queryCmCity(cmCityParam);
    }


    /**
     * 校验参数，检查是否存在重复的编码等
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private void checkParam(CmCityParam cmCityParam, boolean isExcludeSelf) {
        String id = cmCityParam.getCode();
        String code = cmCityParam.getCode();

        // 构建带code的查询条件
        LambdaQueryWrapper<CmCity> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(CmCity::getCode, code);

        // 如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByCode.ne(CmCity::getCode, id);
        }

        // 查询重复记录的数量
        int countByCode = this.count(queryWrapperByCode);

        // 如果存在重复的记录，抛出异常，直接返回前端
        if (countByCode >= 1) {
            throw new ServiceException(CmCityExceptionEnum.CODE_REPEAT);
        }
    }

    /**
     * 获取cm_city
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private CmCity queryCmCity(CmCityParam cmCityParam) {
        CmCity cmCity = this.getById(cmCityParam.getCode());
        if (ObjectUtil.isEmpty(cmCity)) {
            throw new ServiceException(CmCityExceptionEnum.NOT_EXISTED);
        }
        return cmCity;
    }
}